package com.loan.controller.app.apply;

import com.alibaba.fastjson.JSONObject;
import com.loan.common.annotation.Log;
import com.loan.common.core.controller.BaseController;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.domain.Result;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.enums.BusinessType;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.CollectionLogEntity;
import com.loan.system.domain.ExamineLogEntity;
import com.loan.system.domain.RepaymentLogEntity;
import com.loan.system.domain.vo.ExamineUserVo;
import com.loan.system.service.ApplyService;
import com.loan.system.service.ExamineLogService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author shiguang
 * @date 2022年09月13日 21:10
 */
@Validated
@Api(value = "订单管理", tags = {"订单管理"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/app/apply")
public class ApplyController extends BaseController {

    private final ApplyService applyService;

    private final ExamineLogService examineLogService;

    /**
     * 获取订单列表
     */
    @ApiOperation("获取订单列表")
    //@SaCheckPermissio("app:apply:list")
    @Log(title = "获取订单列表", businessType = BusinessType.OTHER)
    @GetMapping("/page")
    public TableDataInfo<ApplyEntity> list(ApplyEntity entity, PageQuery pageQuery,String isRemark, String overStatus, Integer applyType, String isDivide) {
        return applyService.selectPageList(entity, pageQuery, overStatus, applyType, isDivide,isRemark);
    }

    /**
     * 获取放款成功订单列表
     */
    @ApiOperation("获取放款成功订单列表")
    @Log(title = "获取放款成功订单列表", businessType = BusinessType.OTHER)
    @GetMapping("/paySuccess/page")
    public TableDataInfo<ApplyEntity> paySuccessList(ApplyEntity entity, String applyStartTime, String applyEndTime, PageQuery pageQuery) {
        return applyService.selectPagePaySuccessList(entity, applyStartTime, applyEndTime, pageQuery);
    }

    /**
     * 获取订单列表
     */
    @ApiOperation("获取放款失败订单列表")
    @Log(title = "获取放款失败订单列表", businessType = BusinessType.OTHER)
    @GetMapping("/payFail/page")
    public TableDataInfo<ApplyEntity> payFailList(ApplyEntity entity, String applyStartTime, String applyEndTime, PageQuery pageQuery) {
        return applyService.selectPagePayFailsList(entity, applyStartTime, applyEndTime, pageQuery);
    }

    /**
     * 获取待打款订单列表
     */
    @ApiOperation("获取待打款订单列表")
    @Log(title = "获取待打款订单列表", businessType = BusinessType.OTHER)
    @GetMapping("/waitPay/page")
    public TableDataInfo<ApplyEntity> waitPayList(ApplyEntity entity, String applyStartTime, String applyEndTime, PageQuery pageQuery) {
        return applyService.selectPageWaitPayList(entity, applyStartTime, applyEndTime, pageQuery);
    }

    /**
     * 获取还款订单列表
     */
    @ApiOperation("获取还款订单列表")
    @Log(title = "获取还款订单列表", businessType = BusinessType.OTHER)
    @GetMapping("/repaySuccess/page")
    public TableDataInfo<ApplyEntity> repaySuccessList(ApplyEntity entity, PageQuery pageQuery) {
        return applyService.selectPageRepaySuccessList(entity, pageQuery);
    }

    /**
     * 获取拒绝订单
     */
    @ApiOperation("获取拒绝订单")
    @GetMapping("/fail/page")
    public TableDataInfo<ApplyEntity> failList(Long userId, String orderSn, String userName, String userPhone, String status, String overStatus,
                                               String applyType, String createStartTime, String createEndTime,
                                               String examineStartTime, String examineEndTime, PageQuery pageQuery) {
        return applyService.selectPageFailList(userId, orderSn, userName, userPhone, status, overStatus, applyType, createStartTime, createEndTime, examineStartTime, examineEndTime, pageQuery);
    }

    /**
     * 获取待提现用户列表
     */
    @ApiOperation("获取待提现用户列表")
    @Log(title = "获取待提现用户列表", businessType = BusinessType.OTHER)
    @GetMapping("/waitWithdrawal/page")
    public TableDataInfo<ApplyEntity> waitWithdrawallist(ApplyEntity entity, PageQuery pageQuery, String overStatus,String isRemark, Integer applyType, String isDivide) {
        return applyService.selectPageWaitWithdrawallist(entity, pageQuery, overStatus, applyType, isDivide,isRemark);
    }

    @ApiOperation("订单查询")
    //@SaCheckPermissio("app:apply:list")
    @GetMapping("/search")
    public TableDataInfo<ApplyEntity> searchList(String userName,String orderSn, String userPhone, String status, String overStatus, Integer applyType, Long companyId, PageQuery pageQuery) {
        return applyService.selectSearchList(userName,orderSn, userPhone, status, overStatus, applyType, companyId, pageQuery);
    }

    @ApiOperation("待还款订单")
    @GetMapping("/waiPayment/page")
    public TableDataInfo<ApplyEntity> listWaiPayment(ApplyEntity entity, PageQuery pageQuery) {
        return applyService.selectPageListWaiPayment(entity, pageQuery);
    }

    @ApiOperation("获取逾期订单列表")
    @GetMapping("/over/page")
    public TableDataInfo<ApplyEntity> listOver(ApplyEntity entity, String isRemark,PageQuery pageQuery) {
        return applyService.selectPageListOver(entity, isRemark,pageQuery);
    }

    @ApiOperation("获取我的案件列表")
    @Log(title = "获取我的案件列表", businessType = BusinessType.OTHER)
    @GetMapping("/myOver/page")
    public TableDataInfo<ApplyEntity> listMyOver(ApplyEntity entity, String overStatus, String remark, PageQuery pageQuery) {
        return applyService.selectPageListMyOver(entity, overStatus, remark, pageQuery);
    }

    @ApiOperation("获取可催回列表")
    @Log(title = "获取可催回列表", businessType = BusinessType.OTHER)
    @GetMapping("/overRepayment/page")
    public TableDataInfo<ApplyEntity> listOverRepayment(ApplyEntity entity, PageQuery pageQuery) {
        return applyService.selectPageListOverRepayment(entity, pageQuery);
    }

    @ApiOperation("获取用户详情公债订单列表")
    @GetMapping("/listUserInfoApply/page")
    public TableDataInfo<ApplyEntity> listUserInfoApply(ApplyEntity entity, PageQuery pageQuery) {
        return applyService.selectPageListUserInfo(entity, pageQuery);
    }

    @ApiOperation("获取终审订单分配列表")
    @GetMapping("/twoExamine/page")
    public TableDataInfo<ApplyEntity> listTwoExamineApply(ApplyEntity entity, PageQuery pageQuery, String examineStatus) {
        return applyService.selectPageListTwoExamine(entity, pageQuery, examineStatus);
    }

    @ApiOperation("获取终审订单列表")
    @GetMapping("/two/page")
    public TableDataInfo<ApplyEntity> listTwoApply(ApplyEntity entity, PageQuery pageQuery) {
        return applyService.selectPageListTwo(entity, pageQuery);
    }

    @ApiOperation("获取初审订单列表")
    @GetMapping("/one/page")
    public TableDataInfo<ApplyEntity> listOneApply(ApplyEntity entity, PageQuery pageQuery) {
        return applyService.selectPageListFirst(entity, pageQuery);
    }

    @ApiOperation("获取初审订单分配列表")
    @GetMapping("/oneExamine/page")
    public TableDataInfo<ApplyEntity> listOneExamineApply(ApplyEntity entity, PageQuery pageQuery, String examineStatus) {
        return applyService.selectPageListFirstExamine(entity, pageQuery, examineStatus);
    }

    @ApiOperation("获取订单列表")
    @GetMapping("/listApplyCompany")
    public Result<List<ApplyEntity>> listApplyCompany(ApplyEntity entity) {
        return Result.ok(applyService.listApplyCompany(entity));
    }

    @ApiOperation("获取订单列表")
    //@SaCheckPermissio("app:apply:list")
    @GetMapping("/listExamineUser")
    public Result<List<ExamineUserVo>> listExamineUser() {
        return Result.ok(applyService.listExamineUser());
    }

    @ApiOperation("获取订单列表")
    //@SaCheckPermissio("app:apply:list")
    @GetMapping("/listCollectionUser")
    public Result<List<ExamineUserVo>> listCollectionUser() {
        return Result.ok(applyService.listCollectionUser());
    }

    @ApiOperation("获取催收记录")
    //@SaCheckPermissio("app:apply:list")
    @GetMapping("/overLogList")
    public Result<List<CollectionLogEntity>> overLogList(@RequestParam Long userId) {
        return Result.ok(applyService.overLogList(userId));
    }

    @ApiOperation("根据用户ID获取审核记录")
    //@SaCheckPermissio("app:apply:list")
    @GetMapping("/getExamineLogList")
    public Result<List<ExamineLogEntity>> getExamineLogList(@RequestParam Long userId) {
        return Result.ok(examineLogService.getExamineLogList(userId));
    }

    @ApiOperation("获取用户借款记录")
    //@SaCheckPermissio("app:apply:list")
    @GetMapping("/applyLogList")
    public Result<List<ApplyEntity>> applyLogList(@RequestParam Long userId) {
        return Result.ok(applyService.applyLogList(userId));
    }

    @ApiOperation("初审订单分配")
    @Log(title = "初审订单分配", businessType = BusinessType.UPDATE)
    @PutMapping("/updateApplyOneExamineBy")
    public Result<Void> updateApplyOneExamineBy(Long oneExamineBy, @RequestParam String ids) {
        return toAjax(applyService.updateApplyOneExamineBy(oneExamineBy, ids));
    }

    @ApiOperation("复审订单分配")
    @Log(title = "复审订单分配", businessType = BusinessType.UPDATE)
    //@SaCheckPermissio("app:apply:edit")
    @PutMapping("/updateApplyTwoExamineBy")
    public Result<Void> updateApplyTwoExamineBy(Long twoExamineBy, @RequestParam String ids) {
        return toAjax(applyService.updateApplyTwoExamineBy(twoExamineBy, ids));
    }


    @ApiOperation("打款客服分配")
    @Log(title = "打款客服分配", businessType = BusinessType.UPDATE)
    @PutMapping("/updateApplyCutomerBy")
    public Result<Void> updateApplyCutomerBy(Long customerBy, @RequestParam String ids) {
        return toAjax(applyService.updateApplyCutomerBy(customerBy, ids));
    }

    @ApiOperation("催收订单分配")
    @Log(title = "订单管理-催收订单分配", businessType = BusinessType.UPDATE)
    @PutMapping("/updateApplyCollectionBy")
    public Result<Void> updateApplyCollectionBy(Long collectionBy, @RequestParam String ids) {
        return toAjax(applyService.updateApplyCollectionBy(collectionBy, ids));
    }

    @ApiOperation("初审订单审核")
    @Log(title = "初审订单审核", businessType = BusinessType.UPDATE)
    //@SaCheckPermissio("app:apply:update")
    @PutMapping("/examineOneApply")
    public Result<Void> examineOneApply(@RequestParam String ids, String remark, Integer frozenDay) {
        examineLogService.examineOneApply(ids, remark, frozenDay);
        return Result.ok();
    }

    @ApiOperation("批量通过初审订单")
    @Log(title = "批量通过初审订单", businessType = BusinessType.UPDATE)
    @PutMapping("/examineSuccessOneApply")
    public Result<Void> examineSuccessOneApply(@RequestParam String ids) {
        examineLogService.examineSuccessOneApply(ids);
        return Result.ok();
    }

    @ApiOperation("终审订单审核")
    @Log(title = "终审订单审核", businessType = BusinessType.UPDATE)
    @PutMapping("/examineTwoApply")
    public Result<Void> examineTwoApply(@RequestParam String ids, String remark, Integer frozenDay) {
        examineLogService.examineTwoApply(ids, remark, frozenDay);
        return Result.ok();
    }

    @ApiOperation("订单打款")
    @Log(title = "订单打款", businessType = BusinessType.UPDATE)
    //@SaCheckPermissio("app:apply:edit")
    @PutMapping("/payApply")
    public Result<Void> payApply(Long applyId, @RequestParam String type, String bankOrderNo) {
        return toAjax(applyService.payApply(applyId, type, bankOrderNo));
    }


    @ApiOperation("订单还款")
    @Log(title = "订单还款", businessType = BusinessType.UPDATE)
    //@SaCheckPermissio("app:apply:edit")
    @PutMapping("/paymentApply")
    public Result<Void> paymentApply(@RequestParam Long applyId, @RequestParam String type, @RequestParam Integer paymentMoney, @RequestParam String status) {
        return toAjax(applyService.paymentApply(applyId, type, paymentMoney, status));
    }

    /**
     * 关闭订单
     */
    @ApiOperation("关闭订单")
    //@SaCheckPermissio("app:apply:remove")
    @Log(title = "关闭订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/close/{id}")
    public Result<Void> closeApply(@ApiParam("ID") @PathVariable("id") String ids) {
        return toAjax(applyService.closeApply(ids));
    }

    /**
     * 订单回收
     */
    @ApiOperation("订单回收")
    @Log(title = "订单回收", businessType = BusinessType.DELETE)
    @PutMapping("/recovery/{id}")
    public Result<Void> recoveryApply(@ApiParam("ID") @PathVariable("id") Long id) {
        return toAjax(applyService.recoveryApply(id));
    }

    /**
     * 添加记录
     *
     * @param ids
     * @return
     */
    @ApiOperation("添加记录")
    @Log(title = "添加记录", businessType = BusinessType.DELETE)
    @PostMapping("addOverLogForm")
    public Result<Void> addOverLogForm(@RequestBody JSONObject params) {
        return toAjax(applyService.addOverLogForm(params));
    }

    @ApiOperation("查询订单详情")
    @GetMapping("/getInfo")
    public Result<ApplyEntity> getInfo(@RequestParam Long id, @RequestParam Long userId) {
        return Result.ok(applyService.getInfo(id, userId));
    }

    @ApiOperation("订单审核")
    @Log(title = "订单审核", businessType = BusinessType.UPDATE)
    @PostMapping("/examineApply")
    public Result<Void> examineApply(@RequestBody JSONObject jsonObject) {
        return toAjax(examineLogService.examineApply(jsonObject));
    }

    @ApiOperation("获取用户还款记录列表")
    @GetMapping("/getPaymentLogList")
    public TableDataInfo<RepaymentLogEntity> getPaymentLogList(RepaymentLogEntity entity, PageQuery pageQuery) {
        return applyService.selectRepaymentLogList(entity, pageQuery);
    }

    @ApiOperation("撤销还款记录")
    @Log(title = "撤销还款记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/revokeRepaymentLog/{id}")
    public Result<Void> revokeRepaymentLog(@ApiParam("ID") @PathVariable("id") String id) {
        return toAjax(applyService.revokeRepaymentLog(id));
    }

    @ApiOperation("订单跟进")
    @Log(title = "订单跟进", businessType = BusinessType.UPDATE)
    @PutMapping("/saveFollowUpRemarkById")
    public Result<Void> saveFollowUpRemarkById(@RequestParam Long id,@RequestParam String followUpRemark){
        return toAjax(applyService.saveFollowUpRemarkById(id,followUpRemark));
    }
}
